*"* use this source file for the definition and implementation of
*"* local helper classes, interface definitions and type
*"* declarations
CLASS zcl_material_maintain DEFINITION.
  PUBLIC SECTION.

* Attributes
    DATA:lt_matnr TYPE TABLE FOR CREATE i_producttp_2,
         ls_matnr TYPE i_producttp_2.
    DATA:lv_msg_text TYPE string.

    DATA:lv_mtart TYPE i_producttp_2-producttype, "物料类型（大类）
         lv_mstae TYPE i_producttp_2-crossplantstatus,   "物料状态
         lv_matkl TYPE i_producttp_2-productgroup.   "物料组（小类）
    DATA:lv_a(1) TYPE c,
         lv_b(1) TYPE c,
         lv_c(2) TYPE c.

    "物料创建
    METHODS material_create
      IMPORTING
                is_data      TYPE ty_json
      RETURNING VALUE(e_msg) TYPE ty_msg.

    "物料更新
    METHODS  material_update
      IMPORTING
                is_data      TYPE ty_json
      RETURNING VALUE(e_msg) TYPE ty_msg.

ENDCLASS.

CLASS zcl_material_maintain IMPLEMENTATION.
  METHOD material_create.
    " Get current date
    DATA(gv_system_date) = cl_abap_context_info=>get_system_date( ).
    " TODO EML
    CLEAR lv_msg_text.

    DATA(lt_user) = is_data-userfields ." 自定义字段集合
    CLEAR:lv_mtart,lv_a,lv_b,lv_c,lv_matkl.
    CASE is_data-itemsgroupcode.
      WHEN '100'.  "原材料
        lv_mtart = 'ROH'.
        lv_a = '3'.
      WHEN '101'.  "成品
        lv_mtart = 'FERT'.
        lv_a = '1'.
      WHEN '102'.  "半成品
        lv_mtart = 'HALB'.
        lv_a = '2'.
    ENDCASE.

    READ TABLE lt_user INTO DATA(ls_user) WITH KEY fieldname = 'U_ItemStatus'.
    IF sy-subrc = 0.
      IF ls_user-value = '已发布' OR ls_user-value = '预发布'.
        lv_mstae = '03'.  "使用
      ELSEIF ls_user-value = '已废弃'.
        lv_mstae = '05'.  "失效/过期
      ENDIF.
    ENDIF.

    CLEAR:ls_user.
    READ TABLE lt_user INTO ls_user WITH KEY fieldname = 'U_B'.
    IF sy-subrc = 0.
      CASE ls_user-value.
        WHEN '定制电子件'.
          lv_b = '1'.
        WHEN '定制电气件'.
          lv_b = '2'.
        WHEN '定制结构件'.
          lv_b = '3'.
        WHEN '标准电子件'.
          lv_b = '4'.
        WHEN '标准电气件'.
          lv_b = '5'.
        WHEN '标准结构件'.
          lv_b = '6'.
      ENDCASE.
    ENDIF.

    CLEAR:ls_user.
    READ TABLE lt_user INTO ls_user WITH KEY fieldname = 'U_C'.
    IF sy-subrc = 0.
      lv_c = ls_user-value+0(2).
    ENDIF.

    CONCATENATE lv_a lv_b lv_c INTO lv_matkl.

    lt_matnr = VALUE #( ( %cid = 'product1'
                          product = is_data-itemcode     "物料编码
                          %control-product = if_abap_behv=>mk-on
                          producttype = lv_mtart
                          %control-producttype = if_abap_behv=>mk-on
                          baseunit = is_data-invntryuom     "基本单位
                          %control-baseunit = if_abap_behv=>mk-on
                          productgroup = lv_matkl
                          %control-productgroup = if_abap_behv=>mk-on
                          crossplantstatus = lv_mstae
                          %control-crossplantstatus = if_abap_behv=>mk-on
                        ) ).

    MODIFY ENTITIES OF i_producttp_2
           ENTITY product
           CREATE FROM lt_matnr
           CREATE BY \_productdescription
           FIELDS ( language productdescription ) WITH VALUE #( (
           %cid_ref = 'product1'
           product = is_data-itemcode     "物料编码
           %target = VALUE #(
           (
              %cid = 'desc1'
              product = is_data-itemcode
              language = 'E'
              productdescription = is_data-itemname  "物料描述
            )
            (
              %cid = 'desc2'
              product = is_data-itemcode
              language = '1'
              productdescription = is_data-itemname  "物料描述
            )
           )
           ) )

    MAPPED DATA(mapped)
    REPORTED DATA(reported)
    FAILED DATA(failed).

    IF failed IS INITIAL AND mapped IS NOT INITIAL.
      COMMIT ENTITIES
      RESPONSE OF i_producttp_2
      FAILED DATA(failed_commit)
      REPORTED DATA(reported_commit).
      WAIT UP TO 1 SECONDS.
      IF  failed_commit IS INITIAL.
        lv_msg_text = |新增物料数据成功：{ is_data-itemcode } |.
        e_msg-state = '0'.
        e_msg-errormsg = lv_msg_text.
        e_msg-data =  is_data-itemcode.
      ELSE." 事务提交失败
        LOOP AT reported_commit-Product INTO DATA(ls_product).
            IF ls_product-%msg IS NOT INITIAL.      " 检查%msg是否为空（避免空引用错误）
              DATA(lo_msg_head) = CAST if_message( ls_product-%msg ).    "解析消息文本
              lv_msg_text = | { lv_msg_text } ';' { lo_msg_head->get_text( ) } |.
            ELSE.
            ENDIF.
          ENDLOOP.
      ENDIF.
    ELSE.
       LOOP AT reported-Product INTO DATA(ls_pro).
            IF ls_product-%msg IS NOT INITIAL.      " 检查%msg是否为空（避免空引用错误）
              DATA(lo_msg) = CAST if_message( ls_pro-%msg ).    "解析消息文本
              lv_msg_text = | { lv_msg_text } ';' { lo_msg->get_text( ) } |.
            ELSE.
            ENDIF.
          ENDLOOP.
    ENDIF.
  ENDMETHOD.


  METHOD material_update.
    DATA(lt_user) = is_data-userfields ." 自定义字段集合
    CLEAR:lv_mtart,lv_a,lv_b,lv_c,lv_matkl.
    CASE is_data-itemsgroupcode.
      WHEN '100'.  "原材料
        lv_mtart = 'ROH'.
        lv_a = '3'.
      WHEN '101'.  "成品
        lv_mtart = 'FERT'.
        lv_a = '1'.
      WHEN '102'.  "半成品
        lv_mtart = 'HALB'.
        lv_a = '2'.
    ENDCASE.

    READ TABLE lt_user INTO DATA(ls_user) WITH KEY fieldname = 'U_ItemStatus'.
    IF sy-subrc = 0.
      IF ls_user-value = '已发布' OR ls_user-value = '预发布'.
        lv_mstae = '03'.  "使用
      ELSEIF ls_user-value = '已废弃'.
        lv_mstae = '05'.  "失效/过期
      ENDIF.
    ENDIF.

    CLEAR:ls_user.
    READ TABLE lt_user INTO ls_user WITH KEY fieldname = 'U_B'.
    IF sy-subrc = 0.
      CASE ls_user-value.
        WHEN '定制电子件'.
          lv_b = '1'.
        WHEN '定制电气件'.
          lv_b = '2'.
        WHEN '定制结构件'.
          lv_b = '3'.
        WHEN '标准电子件'.
          lv_b = '4'.
        WHEN '标准电气件'.
          lv_b = '5'.
        WHEN '标准结构件'.
          lv_b = '6'.
      ENDCASE.
    ENDIF.

    CLEAR:ls_user.
    READ TABLE lt_user INTO ls_user WITH KEY fieldname = 'U_C'.
    IF sy-subrc = 0.
      lv_c = ls_user-value+0(2).
    ENDIF.

    CONCATENATE lv_a lv_b lv_c INTO lv_matkl.

    MODIFY ENTITIES OF i_producttp_2
           ENTITY product
           UPDATE FIELDS ( producttype baseunit productgroup crossplantstatus )
           WITH VALUE #( ( %key-Product = is_data-itemcode
                           producttype = lv_mtart
                           baseunit = is_data-invntryuom
                           productgroup = lv_matkl
                           crossplantstatus = lv_mstae
           ) )
          ENTITY productdescription
          UPDATE FIELDS ( productdescription )
          WITH VALUE #( ( %key-Product = is_data-itemcode
                %key-language = 'E'
                productdescription = is_data-itemname  "物料描述
           )
           ( %key-Product = is_data-itemcode
             %key-language = '1'
             productdescription = is_data-itemname  "物料描述
           ) )

    MAPPED DATA(mapped)
    REPORTED DATA(reported)
    FAILED DATA(failed).

    IF failed IS INITIAL AND mapped IS NOT INITIAL.
      COMMIT ENTITIES
      RESPONSE OF i_producttp_2
      FAILED DATA(failed_commit)
      REPORTED DATA(reported_commit).
      WAIT UP TO 1 SECONDS.
      IF  failed_commit IS INITIAL.
        lv_msg_text = |更新物料数据成功：{ is_data-itemcode } |.
        e_msg-state = '0'.
        e_msg-errormsg = lv_msg_text.
        e_msg-data =  is_data-itemcode.
      ELSE.
        LOOP AT reported_commit-Product INTO DATA(ls_product).
            IF ls_product-%msg IS NOT INITIAL.      " 检查%msg是否为空（避免空引用错误）
              DATA(lo_msg_head) = CAST if_message( ls_product-%msg ).    "解析消息文本
              lv_msg_text = | { lv_msg_text } ';' { lo_msg_head->get_text( ) } |.
            ELSE.
            ENDIF.
          ENDLOOP.
      ENDIF.
    ELSE.
       LOOP AT reported-Product INTO DATA(ls_pro).
            IF ls_product-%msg IS NOT INITIAL.      " 检查%msg是否为空（避免空引用错误）
              DATA(lo_msg) = CAST if_message( ls_pro-%msg ).    "解析消息文本
              lv_msg_text = | { lv_msg_text } ';' { lo_msg->get_text( ) } |.
            ELSE.
            ENDIF.
          ENDLOOP.
    ENDIF.
  ENDMETHOD.

ENDCLASS.
